﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using CSharpDuino;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;



namespace PomodoroTimer
{
    public class Program
    {
        private static readonly LedProgressBar Progress = new LedProgressBar(0);

        private static readonly CSharpDuino.Button Start = new CSharpDuino.Button(Pins.GPIO_PIN_D4);

        private static readonly CSharpDuino.Button Stop = new CSharpDuino.Button(Pins.GPIO_PIN_D3);

        private static Buzz buzz = new Buzz();

        private const int PomodoroIntrval = 25;
        
        private static DateTime StartTime;
        
        private static DateTime EndTime;

        private static long after;
        
        private static long before;

        public static void Main()
        {
            StartTime = DateTime.MinValue;
            EndTime = DateTime.MinValue;

            Start.ButtonClick += new ButtonClick(Start_ButtonClick);
            Stop.ButtonClick += new ButtonClick(Stop_ButtonClick);

            while (true)
            {
                Start.ReadState();
                Stop.ReadState();


                if (!Progress.Unexpected)
                {
                    if (EndTime > DateTime.Now)
                    {
                        Progress.ProgressValue = Recount();  
                    }

                    if (StartTime > DateTime.MinValue && EndTime < DateTime.Now)
                    {
                        buzz.Beep();
                        for (int i  = 0; i  < 400;  i++)
                        {
                        }
                        buzz.Beep();
                        for (int i = 0; i < 400; i++)
                        {
                        }
                        buzz.Beep();
                        StartTime = DateTime.MinValue;
                    }
                }

                Progress.Display();
                
                Thread.Sleep(10);
            }
        }

        private static long Recount()
        {
            return (Progress.Max / (EndTime.Ticks - StartTime.Ticks)) * (EndTime.Ticks - DateTime.Now.Ticks);
        }

        private static void Start_ButtonClick()
        {
            buzz.Beep();
            StartTime = DateTime.Now;
            EndTime = StartTime.AddMinutes(PomodoroIntrval);
            Progress.Unexpected = false;
            Progress.Min = 0;
            Progress.Max = (EndTime.Ticks - StartTime.Ticks);
            Progress.ProgressValue = Progress.Max;
        }

        private static void Stop_ButtonClick()
        {
            buzz.Beep();
            Progress.Unexpected = !Progress.Unexpected;
            if (Progress.Unexpected)
            {
                after = EndTime.Ticks - DateTime.Now.Ticks;
                before = DateTime.Now.Ticks - StartTime.Ticks;
            }
            else
            {
                EndTime = DateTime.Now.AddTicks(after);
                StartTime = DateTime.Now.AddTicks(-before);
            }
        }
    }
}
